﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using System.IO.Compression;
using System.Xml;
using CustomControls;
using System.Runtime.InteropServices;

namespace Pozr
{
    public partial class MainForm : Form
    {
        SplashForm splashForm;

        XTabControl tabControl;
        ControlStatus controlStatus;
        ControlSettings controlSettings;
        ControlPlugins controlPlugins;
        Controls.ControlDebug controlDebug;

        [DllImport("coredll.dll")]
        static extern int ShowWindow(IntPtr hWnd, int nCmdShow);

        const int SW_MINIMIZED = 6;

        public void Minimize()
        {
            // Since there is no WindowState.Minimize, we have to P/Invoke ShowWindow
            ShowWindow(this.Handle, SW_MINIMIZED);
        }

        public MainForm()
        {
            // Initialize the splash screen
            splashForm = new SplashForm();
            splashForm.Show();

            InitializeComponent();

            Middleware middleware = new Middleware(this);
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            Middleware.ModuleLoadedEvent += new ModuleLoadedEventHandler(Middleware_ModuleLoadedEvent);

            Middleware.OnLogEvent("CLR Version: " + System.Environment.Version.ToString() + "\r\n");
            Middleware.OnLogEvent("Starting up...");

            Middleware.OnLogEvent("Middleware initialized.");

            tabControl = new XTabControl();
            controlSettings = new ControlSettings();
            controlPlugins = new ControlPlugins();
            controlStatus = new ControlStatus();
            controlDebug = new Pozr.Controls.ControlDebug();

            InitModules();
            controlSettings.InitControls();

            InitGUI();
        }

        void Middleware_ModuleLoadedEvent(Interfaces.IModule module)
        {
            module.MainInterface.Name = module.Name;
            splashForm.DisplayIcon(module.IconEnabled);
            splashForm.DisplayMessage(module.Name + " loaded.");
            controlPlugins.AddPluginControl(module.MainInterface);
        }

        private void InitGUI()
        {
            Middleware.MapDisplay.Name = "Map";
            controlSettings.Name = "Settings";
            controlPlugins.Name = "Plugins";
            controlDebug.Name = "#DEBUG#";
            controlStatus.Height = 60;
            controlStatus.Dock = DockStyle.Top;
            tabControl.Add(Middleware.MapDisplay);
            tabControl.Add(controlSettings);
            tabControl.Add(controlPlugins);
            tabControl.Add(controlDebug);
            tabControl.Dock = DockStyle.Fill;
            this.Controls.Add(tabControl);

            Middleware.OnLogEvent("Ready.");
            tabControl.SelectItem(0);
            this.Controls.Add(controlStatus);
            tabControl.BringToFront();

            splashForm.Dispose();
        }

        private void InitModules()
        {
            Middleware.PositionHandler.LoadModules();

            //Call the find plugins routine, to search in our Plugins Folder
            Middleware.PluginServices.LoadModules();

            //Add each plugin to the middleware interface
            foreach (Types.Plugin pluginOn in Middleware.PluginServices.Plugins)
            {
                splashForm.DisplayIcon(pluginOn.Instance.IconEnabled);
                splashForm.DisplayMessage(pluginOn.Instance.Name + " loaded.");

                Middleware.OnLogEvent("Plugin loaded: " + pluginOn.Instance.Name);
                pluginOn.Instance.MainInterface.Name = pluginOn.Instance.Name;
                controlPlugins.AddPluginControl(pluginOn.Instance.MainInterface);
            }
        }

        public void splash_displayModuleIcon(AlphaImage icon)
        {
            if (splashForm != null)
            {
                splashForm.DisplayIcon(icon);
            }
        }

        public void splash_displayStatusMessage(string s)
        {
            if (splashForm != null)
            {
                splashForm.DisplayMessage(s);
            }
        }

        private void MainForm_Activated(object sender, EventArgs e)
        {
            this.Show();
        }
   }
}